package main

import (
	"fmt"
	"log"
	"smartposts/internal/config"
	"smartposts/internal/global"
	"smartposts/internal/mq"
	"smartposts/internal/router"
)

func main() {
	configPath := "./configs/config.yml"
	err := config.LoadConfig(configPath)
	if err != nil {
		log.Fatalf("failed to load config: %v", err)
	}
	log.Printf("Load config: %v\n", config.AppConfig)

	config.InitDB()
	log.Println("Init database succeeded.")

	config.InitRedis()
	log.Println("Init redis succeeded.")
	defer global.RedisDB.Close()

	mq.InitMQ()
	log.Println("Init rabbitmq succeeded.")
	defer mq.MQ.Close()

	r := router.SetupRouter()
	address := fmt.Sprintf("%s:%d", config.AppConfig.Server.Host, config.AppConfig.Server.Port)
	log.Printf("Starting server on %s", address)
	if err := r.Run(address); err != nil {
		log.Fatalf("failed to start server: %v", err)
	}
}
